<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
// 
//  login.php
//  phpweb
//  
//  Created by WillLee on 2011-06-28.
//  Copyright 2011 lishuzu. All rights reserved.
// 


	function __construct()
	{
		parent::__construct();
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'login', 2);
		}
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
		$this->uid = $this->uri->segment(3) ? intval($this->uri->segment(3)) : $this->user['uid'];
		if (!$this->uid) {
			showmessage('出错了。您访问的用户不存在！');
		}
		$this->load->model('user_model');
		$this->load->model('home_model');
		$this->load->model('ship_model');
		$this->load->model('talk_model');
		$this->load->model('feed_model');

	}
	
	public function index()
	{	
		$data = array();
		$data['myworks'] = $this->home_model->getworks();
		$data['userinfo'] = $this->user_model->get_user_smart($this->uid);
		$data['self'] = $this->security_model->checkself($this->uid);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($this->uid);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		
		//查询关注人列表
		if ($data['self']) {
			$followuids = $this->ship_model->get_followuids($this->user['uid']);
			//把自己也加进去
			$followuids[] = $this->user['uid'];
		}else{
			$followuids = $this->uid;
		}
		$feeds = $this->home_model->get_feeds($followuids);
		if ($feeds['count']) {
			$data['feeds'] = $feeds['list'];
			$data['count'] = $feeds['count'];
			$data['multi'] = $feeds['multi'];
		}

		
		$data['followings'] = $this->ship_model->get_follows($this->uid, '20');
		$this->load->template('home_tpl',$data);
	}
	
	public function talk(){
		if (submitcheck('talksubmit')) {
			$tdayetalk = $this->input->post('tdayetalk')? $this->input->post('tdayetalk', true) : '';
			if (!$this->user) {
				showmessage('您还没有登录，不能进行我说操作', 'home/index', 2);
			}
			if ($tdayetalk) {
				$insertdata = array(
					'uid' => $this->user['uid'],
					'name' => $this->user['name'],
					'dateline' => now(),
					'message' => $tdayetalk,
					'ip' => getonlineip()
					);
				$talkid = $this->talk_model->add($insertdata);
				// 发送feed
				$this->feed_model->feed_publish($talkid, 'talkid', 1);
				redirect(base_url().'home/index/', 'refresh');
			}else{
				showmessage('我说内容不能未空', 'home/index', 2);				
			}
		}
	}
}

/* End of file index.php */
/* Location: ./application/controllers/index.php */